package com.scpii.universal.pns.service;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.dao.CatelogDao;
import com.scpii.universal.hibernate.dao.ContentDao;
import com.scpii.universal.hibernate.entity.CatelogTab;
import com.scpii.universal.hibernate.entity.ContentTab;
import com.scpii.universal.pns.dao.EcProductDao;
import com.scpii.universal.pns.dao.UserActionsDao;
import com.scpii.universal.pns.dao.UsersDao;
import com.scpii.universal.pns.entity.EcProduct;
import com.scpii.universal.pns.entity.UserActions;
import com.scpii.universal.pns.entity.Users;

@Service
public class UserActionsServiceImpl extends
		PnsBaseServiceImpl<UserActions, Long> implements UserActionsService {
	@Resource
	private UserActionsDao userActionsDao;
	@Resource
	private ContentDao contentDao;
	@Resource
	private CatelogDao catelogDao;
	@Resource
	private EcProductDao ecProductDao;
	@Resource
	private UsersDao usersDao;

	@Resource
	public void setBaseDao(UserActionsDao userActionsDao) {
		super.setBaseDao(userActionsDao);
	}

	@Override
	public Integer getUserActionCountByType(Integer appId, Long targetId,
			String actionType) {
		Map<String, Object> map = new HashMap<String, Object>();
		StringBuffer hql = new StringBuffer(
				" select count(um.id) from UserActions um,Users u where u.id=um.userId and u.appId=:appId ");
		if ("FAV".equals(actionType)) {
			hql.append(" and  um.actionType in('ACTIVITIES_FAV','EC_FAVORITE') ");
		} else {
			hql.append(" and um.actionType=:actionType ");
			map.put("actionType", actionType);
		}
		hql.append(" order by um.createDt desc");
		map.put("appId", GenericConstants.APP_ID_PRE + appId);

		List<Long> list = userActionsDao.findByHqlCount(hql.toString(), 0, 0,
				map, null);
		if (list != null && list.size() == 1) {
			return list.get(0).intValue();
		} else {
			return 0;
		}
	}

	@Override
	public List<UserActions> getUserActionsByUserId(Long userId,
			Integer pageNo, Integer pageSize) {
		StringBuffer hql = new StringBuffer(" select u from UserActions u "
				+ "where u.userId=:userId  order by u.id desc");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		List<UserActions> list = userActionsDao.findByHql(hql.toString(),
				pageNo, pageSize, map, null);

		return list;
	}

	@Override
	public List<UserActions> getUserActionsByActionType(Integer appId,
			Long targetId, String actionType, Integer pageNo, Integer pageSize) {
		Map<String, Object> map = new HashMap<String, Object>();
		StringBuffer hql = new StringBuffer(
				" select um from UserActions um,Users u where u.id=um.userId and u.appId=:appId ");
		if ("FAV".equals(actionType)) {
			hql.append(" and  um.actionType in('ACTIVITIES_FAV','EC_FAVORITE') ");
		} else {
			hql.append(" and um.actionType=:actionType ");
			map.put("actionType", actionType);
		}
		if (targetId != null) {
			hql.append(" and um.targetId=:targetId ");
			map.put("targetId", targetId);
		}
		hql.append(" order by um.createDt desc");
		map.put("appId", GenericConstants.APP_ID_PRE + appId);

		List<UserActions> list = userActionsDao.findByHql(hql.toString(),
				pageNo, pageSize, map, null);
		String imgPath = ResourceBundle.getBundle(
				GenericConstants.GLOBAL_PROPERTIES_PATH, Locale.getDefault())
				.getString("pns.img.path");
		if (list != null && list.size() > 0) {
			for (UserActions userAction : list) {
				getEventContent(userAction.getTargetId(), userAction);
				Users user = usersDao.get(userAction.getCreateBy());
				if (user != null) {
					userAction.setUserName(user.getUserAilas());
					userAction.setUserPhone(user.getPhoneNumber());
					userAction.setAvator(user.getAvatar());
					if (user.getAvatar() != null
							&& !user.getAvatar().startsWith("http://")) {
						userAction.setAvator(imgPath + user.getAvatar());
					}
				}
			}
		}
		return list;
	}

	/**
	 * 查询action时间的发生点
	 * 
	 * @param action
	 * @return
	 */
	private void getEventContent(Long targetId, UserActions userActio) {
		String eventContent = "";
		String eventFrom = "";
		if (targetId.toString().startsWith("37")) {
			EcProduct pro = ecProductDao.get(targetId);
			eventContent = pro.getTitle();
			eventFrom = "电商";
		} else if (targetId.toString().startsWith("48")) {
			ContentTab tab = contentDao.get(new Long(targetId - 480000000000l)
					.intValue());
			if (tab != null) {
				eventContent = tab.getVarContentName();
				CatelogTab cateLog = catelogDao.get(tab.getIntCateId());
				if (cateLog != null) {
					eventFrom = cateLog.getVarCateName();
				}
			}
		}

		userActio.setActionsCate(eventFrom);
		userActio.setActionsTitle(eventContent);

	}
}
